package com.study;

/**
 * @Classname Starters
 * @Version 1.0.0
 * @Date 2022/4/2 12:23
 * @Created by yd
 */

import lombok.extern.slf4j.Slf4j;
import org.h2.tools.Server;

import java.sql.SQLException;

@Slf4j
public class Starters {


	private Server server;
	//数据库端口
	private String port = "8080";
	//数据库目录
	//数据库文件是当前目录下的freebytes文件，这里用的是相对路径，也可以使用本地绝对路径，如d://test/freebytes
	private String dbDir = "./freebytes;AUTO_SERVER=TRUE;DATABASE_TO_UPPER=FALSE;IFEXISTS=false;MVCC=true";
	//数据库用户名
	private String user = "test";
	//数据库密码
	private String password = "test";

	public static void startH2Server() {
		try {
			Server h2Server = Server.createTcpServer(new String[]{"-tcp", "-tcpAllowOthers", "-ifNotExists", "-tcpPort", "9999"}).start(); // 关键代码
			if (h2Server.isRunning(true)) {
				log.info("H2 server was started and is running.");
			} else {
				throw new RuntimeException("Could not start H2 server.");
			}
		} catch (SQLException e) {
			throw new RuntimeException("Failed to start H2 server: ", e);
		}
	}

	public void startH2ServerPort() {
		try {
			System.out.println("启动h2...");
			server = Server.createTcpServer(new String[]{"-tcp", "-tcpAllowOthers", "-tcpPort", port}).start();
		} catch (SQLException e) {
			System.out.println("启动h2出错：" + e.toString());
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
}
